import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.*;

public class GUIchannel{
	private GUI gui;
	private String channel;
	private JTextArea textArea;
	private JTextField msgField;
	JList list;
	DefaultListModel listModel;
	
	public GUIchannel(GUI gui, String channel) {
		this.gui = gui;
		this.channel = channel;
	}
	
	public JSplitPane createChannel() {
		//Creates the participants list
	    listModel = new DefaultListModel();
	        
	    list = new JList(listModel);
	    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );
	    list.setLayoutOrientation(JList.VERTICAL);
	    list.setVisibleRowCount(-1);
	    list.addMouseListener(new GUImouse(list, gui));
	        
	    JScrollPane listScroller = new JScrollPane(list);
	    listScroller.setPreferredSize(new Dimension(100, 300));
	        
	    //Creates the chatfeed
	    textArea = new JTextArea(20, 20);
	    textArea.setEditable(false);
	    
	    JScrollPane textScroller = new JScrollPane(textArea); 
		textScroller.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {  
			public void adjustmentValueChanged(AdjustmentEvent e) {  
				e.getAdjustable().setValue(e.getAdjustable().getMaximum());  
				}}); 
	        
	    //Creates the top SplitPane between textScroller and listScroller
	    JSplitPane topPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, textScroller, listScroller);
	    topPane.setOneTouchExpandable(true);
	    topPane.setDividerLocation(300);
		
	    //Provide minimum sizes for the two components in the split pane
	    textScroller.setMinimumSize(new Dimension(300, 300));
	    listScroller.setMinimumSize(new Dimension(100, 300));
		
	    //Creates the messagePane
	    JPanel msgPane = new JPanel(new FlowLayout());
	    JLabel msgLabel = new JLabel("Message");
	    msgField = new JTextField(24);
	    JButton msgButtonSend = new JButton("Send");
	    JButton msgButtonClose = new JButton("Close");
	    msgPane.add(msgLabel);
	    msgPane.add(msgField);
	    msgPane.add(msgButtonSend);
	    msgPane.add(msgButtonClose);

	    msgField.addActionListener(new GUIkeyboard(this, gui));
	    msgButtonSend.addActionListener(new GUImsgButton(this, gui));
	    msgButtonClose.addActionListener(new GUImsgButton(this, gui));
	    
	    //Creates the main SplitPane between msgPane and topPane
	    JSplitPane mainPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPane, msgPane);
	    topPane.setOneTouchExpandable(true);
	    topPane.setDividerLocation(400);
		
	    //Provide minimum sizes for the two components in the split pane
	    topPane.setMinimumSize(new Dimension(500, 400));
	    msgPane.setMinimumSize(new Dimension(500, 100));
	    
	    return mainPane;
	}
	
	public String getChannelName() {
		return channel;
	}
	
	public void setName(String name) {
		channel = name;
	}
	
	public void appendTextArea(String s) {
		textArea.append(s + "\n");
	}
	
	public void sendMsg() {
		String msg = msgField.getText();
		if (msg.length() > 512) {
			msg = msg.substring(0, 512);
		} 		
		gui.getClient().commandc.privmsg(channel, msg);
		msgField.setText("");
	}

	public void addUser(String user) {
		listModel.addElement(user);
	}
	
	public void removeUser(String user) {
		if(listModel.contains(user)) {
			listModel.removeElement(user);
		}
		else if(listModel.contains('@' + user)){
			listModel.removeElement('@' + user);
		}
		else if(listModel.contains('+' + user)){
			listModel.removeElement('+' + user);
		}
	}

	public boolean hasUser(String user) {
		return listModel.contains(user) || listModel.contains('@' + user) ||
			   listModel.contains('+' + user);
	}
	
	public void changeUser(String oldUser, String newUser) {
		if(listModel.contains(oldUser)) {
			listModel.setElementAt(newUser, listModel.indexOf(oldUser));
		}
		else if(listModel.contains('@' + oldUser)){
			listModel.setElementAt('@' + newUser, listModel.indexOf('@' + oldUser));
		}
		else if(listModel.contains('+' + oldUser)){
			listModel.setElementAt('+' + newUser, listModel.indexOf('+' + oldUser));
		}
	}
	
	public void addOp(String user){
		if (listModel.contains(user)){
			listModel.setElementAt('@' + user, listModel.indexOf(user));
			
		}else if(listModel.contains('+' + user)){
			listModel.setElementAt('@' + user, listModel.indexOf('+' + user));
		}
	}
	
	public void removeOp(String user){
		if(listModel.contains('@' + user)){
			listModel.setElementAt(user, listModel.indexOf('@' + user));
		}
		
	}
	
	public void addVoice(String user){
		if (listModel.contains(user)){
			listModel.setElementAt('+' + user, listModel.indexOf(user));
		}
	}
	
	public void removeVoice(String user){
		if (listModel.contains('+' + user)){
			listModel.setElementAt(user, listModel.indexOf('+' + user));
		}
	}
	
}
